﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingBll
{
    public class ReturnExchangeBll : BaseDeleteBll<ReturnExchange>, IReturnExchangeBll
    {
        private IReturnExchangeDal _returnExchangeDal;
        private IOrderInfoDal _orderInfoDal;
        private IUserInfoDal _userInfoDal;
        private ICommodityInfoDal _commodityInfoDal;
        private ShoppingDbContext _dbContext;


        public ReturnExchangeBll(ShoppingDbContext dbContext,ICommodityInfoDal commodityInfoDal,IUserInfoDal userInfoDal,IReturnExchangeDal returnExchangeDal,IOrderInfoDal orderInfoDal) : base(returnExchangeDal)
        {
            _returnExchangeDal = returnExchangeDal;
            _orderInfoDal = orderInfoDal;
            _userInfoDal = userInfoDal;
            _commodityInfoDal = commodityInfoDal;
            _dbContext = dbContext;
        }



        /// <summary>
        /// 获取退货分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="state"></param>
        /// <param name="goodsId"></param>
        /// <returns></returns>
        public object GetReturnExchangeListByPage(int page, int limit, out int count, ReturnExchangeStatusEnum state, string CommodityName)
        {

            var Temp = from r in _returnExchangeDal.GetEntitiesDb().Where(u => u.IsDelete == false)
                       join o in _orderInfoDal.GetEntitiesDb()
                       on r.OrderId equals o.ID into roTemp
                       from ro in roTemp.DefaultIfEmpty()

                       join c in _commodityInfoDal.GetEntitiesDb()
                       on r.GoodsId equals c.ID into rcTemp
                       from rc in rcTemp.DefaultIfEmpty()

                       join u in _userInfoDal.GetEntitiesDb()
                       on r.UserId equals u.ID into ruTemp
                       from ru in ruTemp.DefaultIfEmpty()
                       select new
                       {
                           r.ID,
                           r.OrderId,
                           ru.UserName,
                           rc.CommodityName,
                           ro.Num,
                           ro.Amount,
                           r.Reason,
                           r.Commodityimg,
                           r.State,
                           r.CreateTime
                           
                       };

            if (state != 0)
            {
                Temp = Temp.Where(t => t.State == state);
            }

            if (!string.IsNullOrEmpty(CommodityName))
            {
                Temp = Temp.Where(t => t.CommodityName.Contains(CommodityName));
            }


            //查询出来数据的数量
            count = Temp.Count();


            //分页
            Temp = Temp.OrderBy(c => c.CreateTime).Skip((page - 1) * limit).Take(limit);


            var list = Temp.ToList().Select(c => 
            {
                string State = c.State.ToString();
                return new
                {

                    c.ID,
                    c.OrderId,
                    c.UserName,
                    c.CommodityName,
                    c.Num,
                    c.Amount,
                    c.Reason,
                    c.Commodityimg,
                    State, 
                    CreateTime = c.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                };

            });

            return list;
        }


        /// <summary>
        /// 获取退货状态下拉
        /// </summary>
        /// <returns></returns>
        public object GetStatusSelectOption()
        {
            List<object> options = new List<object>();
            //获取枚举中所有的名字
            var names = Enum.GetNames(typeof(ReturnExchangeStatusEnum));

            foreach (var namesItem in names)
            {
                var value = (int)Enum.Parse(typeof(ReturnExchangeStatusEnum), namesItem);
                options.Add(new
                {
                    Key = namesItem,
                    Value = value
                });
            }
            return options;
        }


        /// <summary>
        /// 同意申请退货
        /// </summary>
        /// <param name="returnExchangeId"></param>
        /// <returns></returns>
        public bool ConsentApply(string returnExchangeId)
        {

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                ReturnExchange returnExchange = _returnExchangeDal.GetEntity(returnExchangeId);
                returnExchange.State = ReturnExchangeStatusEnum.同意退款;
                returnExchange.HandleTime = DateTime.Now;
                bool isSucces1 = _returnExchangeDal.Update(returnExchange);


                OrderInfo orderInfo = _orderInfoDal.GetEntity(returnExchange.OrderId);
                orderInfo.State = OrderInfo.OrderStateEnum.同意退货;
                bool isSuccess2 = _orderInfoDal.Update(orderInfo);
                if (isSucces1 && isSuccess2)
                {
                    transaction.Commit();
                    return true;
                }
                else
                {
                    transaction.Rollback();
                    return false;
                }
            };


        }


    }
}
